package com.xuanxuan.socketdemo.activity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xuanxuan.socketdemo.R;
import com.ywicc.commlibs.udp.UdpUtils;

import java.net.DatagramPacket;
import java.nio.charset.StandardCharsets;

/**
 * author : zhouxuan77
 * e-mail : zhouxuan77@163.com
 * time   : 2022/04/01
 * desc   :
 * version: 1.0
 */
public class UdpTestActivity extends AppCompatActivity {

    private UdpUtils udpUtils;

    private int tmpInt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.udp_test_activity);

        udpUtils = new UdpUtils();
        tmpInt = 0;
    }

    public void btn_send(View view) {
        udpUtils.onBroadcastSend(("hello World: " + tmpInt).getBytes(StandardCharsets.UTF_8));
        tmpInt++;
    }

    public void btn_receive(View view) {
        udpUtils.onBroadcastReceive(new UdpUtils.UdpUtilsImpl() {
            @Override
            public void callBackData(int object, DatagramPacket data) {
                Log.w("UdpTestActivity", new String(data.getData(), 0, data.getLength()));
            }
        }, 0);
    }
}
